package com.autotest.sync;

import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import com.autotest.sync.android.Sync;
import com.autotest.sync.android.Sync.DeviceChangeListener;
import com.autotest.sync.utils.ADBHelper;
import com.autotest.sync.utils.ThreadManager;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import java.awt.FlowLayout;
import java.io.File;
import java.util.Map;

/**
 * 顶部手机选择及操作区域
 * @author zy
 *
 */
public class TopPanel extends JPanel implements ActionListener,DeviceChangeListener {
	private static final long serialVersionUID = -3127100916969669023L;
	private DefaultComboBoxModel<String> deviceModel = new DefaultComboBoxModel<String>();
	private Sync sync = Sync.getInstance();
	private JComboBox<String> mCbDevice;
	private JButton mBtnStart;
	private JButton mBtnPause;
	private JButton mBtnStop;
	private JLabel lblNewLabel;
	private JPanel panel_device;
	private JPanel panel_apk;
	private JTextField tfApkText;
	private JButton btnSelect;
	private JLabel lblApkPath;
	private JButton btnInstallAll;
	private JButton btnNewButton_1;
	
	private ClearEvent mClearEvent;
	private JPanel panel_input;
	private JLabel lblNewLabel_1;
	private JTextField mInputEvent;
	private JLabel lblNewLabel_2;
	private JButton btnlog;
	public interface ClearEvent{
		public void clear();
	}

	/**
	 * Create the panel.
	 */
	public TopPanel() {
		sync.addListener(this);
		setLayout(new GridLayout(3, 1, 0, 0));
		
		panel_apk = new JPanel();
		add(panel_apk);
		panel_apk.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		
		lblApkPath = new JLabel("apk path:");
		panel_apk.add(lblApkPath);
		
		tfApkText = new JTextField();
		panel_apk.add(tfApkText);
		tfApkText.setColumns(20);
		
		btnSelect = new JButton("...");
		btnSelect.addActionListener(this);
		panel_apk.add(btnSelect);
		
		btnInstallAll = new JButton("安装所有");
		btnInstallAll.addActionListener(this);
		panel_apk.add(btnInstallAll);
		
		panel_input = new JPanel();
		panel_input.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		add(panel_input);
		
		lblNewLabel_1 = new JLabel("输入事件");
		panel_input.add(lblNewLabel_1);
		
		mInputEvent = new JTextField();
		panel_input.add(mInputEvent);
		mInputEvent.setColumns(20);
		
		lblNewLabel_2 = new JLabel("自定义事件类型");
		panel_input.add(lblNewLabel_2);
		
		btnlog = new JButton("关闭日志");
		panel_input.add(btnlog);
		btnlog.addActionListener(this);
		
		panel_device = new JPanel();
		panel_device.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		add(panel_device);
		lblNewLabel = new JLabel("手机序列号: ");
		panel_device.add(lblNewLabel);
		
		mCbDevice = new JComboBox<String>();
		panel_device.add(mCbDevice);
		mCbDevice.setPreferredSize(new Dimension(150,20));
		
		btnNewButton_1 = new JButton("安装");
		btnNewButton_1.addActionListener(this);
		panel_device.add(btnNewButton_1);
		
		mBtnStart = new JButton("开始");
		panel_device.add(mBtnStart);
		
		mBtnPause = new JButton("暂停");
		panel_device.add(mBtnPause);
		mBtnPause.setEnabled(false);
		
		mBtnStop = new JButton("结束");
		panel_device.add(mBtnStop);
		mBtnStop.setEnabled(false);
		mBtnStop.addActionListener(this);
		mBtnPause.addActionListener(this);
		mBtnStart.addActionListener(this);
		updateDevice();
	}
	
	private void updateDevice(){
		String[] devices = sync.getOnlineDevices();
		deviceModel.removeAllElements();
		if (devices != null) {
			for (String device : devices) {
				deviceModel.addElement(device);
			}
		}
		mCbDevice.setModel(deviceModel);
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmdStr = e.getActionCommand();
		switch(cmdStr){
		case "开始":
			if(deviceModel.getSize() < 2){
				JOptionPane.showMessageDialog(null, "检查设备连接，设备数需大于2");
				return;
			}
			Object obj = deviceModel.getSelectedItem();
			if(Strings.isNullOrEmpty(mInputEvent.getText())){
				sync.setMaster(obj.toString(), null);
			}else{
				sync.setMaster(obj.toString(), mInputEvent.getText());
			}
			sync.start();
			mCbDevice.setEnabled(false);
			mBtnPause.setEnabled(true);
			mBtnStop.setEnabled(true);
			mBtnStart.setEnabled(false);
			if(mClearEvent != null){
				mClearEvent.clear();
			}
			break;
		case "结束":
			mCbDevice.setEnabled(true);
			mBtnStart.setEnabled(true);
			updateDevice();
			sync.stop();
			break;
		case "暂停":
			sync.switchPause();
			mBtnPause.setText("恢复");
			break;
		case "恢复":
			mBtnPause.setText("暂停");
			sync.switchPause();
			break;
		case "关闭日志":
			btnlog.setText("开启日志");
			Config.Flag_Log = false;
			break;
		case "开启日志":
			btnlog.setText("关闭日志");
			Config.Flag_Log = true;
			break;
		case "...":
			JFileChooser jfc = new JFileChooser();
			jfc.setFileFilter(new FileFilter() {
				
				@Override
				public String getDescription() {
					// TODO Auto-generated method stub
					return "*.apk";
				}
				
				@Override
				public boolean accept(File f) {
					return f.getName().endsWith(".apk") ||f.isDirectory();
				}
			});
			jfc.showDialog(new JLabel(), "选择apk");
			File file = jfc.getSelectedFile();
			if(file != null){
				tfApkText.setText(file.getAbsolutePath());
			}
			break;
		case "安装所有":
			for(int i=0; i<deviceModel.getSize(); i++){
				String serial = deviceModel.getElementAt(i);
				installApk(serial);
			}
			break;
		case "安装":
			Object obj2 = deviceModel.getSelectedItem();
			ThreadManager.getInstance().start(()->{
				if(obj2 == null || !Strings.isNullOrEmpty(tfApkText.getText())){
					installApk(obj2.toString());
				}
			});
			break;
		default:
			break;
		}
	}
	
	private void installApk(String serial){
		ThreadManager.getInstance().start(()->{
			if(serial == null || !Strings.isNullOrEmpty(tfApkText.getText())){
				System.out.println("序列号: " + serial + " 正在安装");
				Map<String, String> result = ADBHelper.installApk(serial.toString(), tfApkText.getText());
				if(result != null){
					if(Objects.equal("0", result.get("status"))){
						System.out.println("序列号: " + serial + " 安装完成");
						return;
					}
				}
				System.out.println("序列号: " + serial + " 安装失败");
			}
		});
	}

	@Override
	public void add(String serial) {
		deviceModel.addElement(serial);
		if(sync.getStatus() == 0){
			updateDevice();
		}
	}

	@Override
	public void remove(String serial) {
		deviceModel.removeElement(serial);
		if(sync.getStatus() == 0){
			updateDevice();
		}
	}
	
	public void register(ClearEvent clearEvent){
		mClearEvent = clearEvent;
	}

	
}
